//
//  ViewController.m
//  网易新闻
//
//  Created by 王宇先 on 16/4/8.
//  Copyright © 2016年 王宇先. All rights reserved.
//

#import "ViewController.h"
#import "HomeCollectionViewCell.h"
#import "AudioCollectionViewCell.h"
#import "SportCollectionViewCell.h"
#import "MoneyCollectionViewCell.h"
#import "HotCollectionViewCell.h"
#import "NetHandle.h"
#import "LayoutCollectionView.h"
#import "HomePageModel.h"
#import "CollectionViewOfNews.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) LayoutCollectionView *flowout;
@property (nonatomic, retain) CollectionViewOfNews *collection;
@property (nonatomic, retain)NSMutableArray *arrOfmodel;
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)NSMutableArray *buttonArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"新闻";
    self.view.backgroundColor = [UIColor cyanColor];
    //创建新闻的头部标题scrollview
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    scroll.backgroundColor = [UIColor whiteColor];
    NSArray *array = @[@"头条", @"娱乐", @"科技", @"体育", @"财经"];
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(20 + 60 *i
                                  , 0, scroll.frame.size.width / 5, scroll.frame.size.height);
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = 100 + i;
        [self.buttonArray addObject:button];
        [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:button];
        
    }
    scroll.tag = 1000;
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.delegate = self;
    scroll.contentSize = CGSizeMake(self.view.frame.size.width, 0);
    scroll.contentOffset = CGPointMake(0, 64);
    [self.view addSubview:scroll];
    [self.view addSubview:self.collectionView];
    [self handledata];
}
#pragma mark - handledata
- (void)handledata{
    
    [NetHandle handleDataWithUrl:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html?from=toutiao&passport=&devId=x5%2BSswSS0UhQ6mO8rpqsSoLg4r76JP0mXhMHVg5DE4uh60X8TbfN6XM9ludBdhOC&size=20&version=5.6.0&spever=false&net=wifi&lat=7eW%2F5%2F8o5hlEi%2Fot5rNTsw%3D%3D&lon=d4Nvc%2FwkfS6tuLYTwGx4Dw%3D%3D&ts=1460108771&sign=Ren6fX4TjmDWjOsIVtLCAhcXYPtCtK7kOTCU6ve%2F3kt48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstore" completion:^(id result, NSURLResponse *response, NSError *error) {
        //NSLog(@"%@", result);
        NSArray *arr = [result objectForKey:@"T1348647853363"];
        
        self.arrOfmodel = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
            HomePageModel *model = [[HomePageModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfmodel addObject:model];
            //NSLog(@"self:%@", self.arrOfmodel);
            
        }
        [self.collection reloadData];
    }];
    
}
#pragma mark - 创建 ColletionView
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
        flowLaout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
        flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLaout.minimumLineSpacing = 0;
        flowLaout.minimumInteritemSpacing = 0;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 97, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLaout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor cyanColor];
        [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
        [_collectionView registerClass:[AudioCollectionViewCell class] forCellWithReuseIdentifier:@"AudioCollectionViewCell"];
        [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"HotCollectionViewCell"];
        [_collectionView registerClass:[SportCollectionViewCell class] forCellWithReuseIdentifier:@"SportCollectionViewCell"];
        [_collectionView registerClass:[MoneyCollectionViewCell class]forCellWithReuseIdentifier:@"MoneyCollectionViewCell"];
        //        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return _collectionView;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
        return cell;
    } else if (indexPath.item == 1) {
        AudioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AudioCollectionViewCell" forIndexPath:indexPath];
        return cell;
    } else if (indexPath.item == 2) {
        HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCollectionViewCell" forIndexPath:indexPath];
        return cell;
    } else if (indexPath.item == 3) {
        SportCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SportCollectionViewCell" forIndexPath:indexPath];
        return cell;
    } else if (indexPath.item == 4){
        MoneyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoneyCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    UIButton *button = (UIButton *)[self.view viewWithTag:100 + self.collectionView.contentOffset.x / 375];
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    for (int i = 0; i < _buttonArray.count; i ++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:100 + i];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.collectionView.contentOffset.x == 0) {
        UIButton *button = (UIButton *)[self.view viewWithTag:100];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    //    NSLog(@"%f", self.collectionView.contentOffset.x);
}

#pragma mark - scorellview 上的 button 方法
- (void)buttonDidPress:(UIButton *)sender{
    [self.collectionView setContentOffset:CGPointMake((sender.tag - 100)*375, -64)];
    for (int i = 0; i < _buttonArray.count; i ++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:100 + i];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        self.buttonArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _buttonArray;
}
#pragma mark - collectionview
- (void)layoutCollectionView{
    self.flowout = [[LayoutCollectionView alloc]init];
    
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
